#!/bin/bash -e

# Build and package Docker image of Memgraph.

function print_help () {
    echo "Usage: $0 [--latest] MEMGRAPH_PACKAGE.(deb|rpm)"
    echo "Optional arguments:"
    echo -e "\t-h|--help\t\tPrint help."
    echo -e "\t--latest\t\tTag image as latest version."
}

working_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

latest_image=""
tag_latest=""
toolchain_version="v6"
package_path=""
arch=""
src_path=""
custom_mirror=""
generate_sbom=false
while [[ $# -gt 0 ]]; do
  case "$1" in
    --latest)
        latest_image="memgraph/memgraph:latest"
        tag_latest="-t memgraph/memgraph:latest"
        shift
    ;;
    --package-path)
        package_path=$2
        shift 2
    ;;
    --src-path)
        src_path=$2
        shift 2
    ;;
    --toolchain)
        toolchain_version=$2
        shift 2
    ;;
    --arch)
        arch=$2
        shift 2
    ;;
    -h|--help)
        print_help
        exit 0
    ;;
    --custom-mirror)
        custom_mirror=$2
        shift 2
    ;;
    --generate-sbom)
        generate_sbom=$2
        shift 2
    ;;
    *)
        echo -e "Error: Unknown option '$1'"
        exit 1
    ;;
  esac
done
echo "latest_image: $latest_image, latest_tag: $latest_tag, package_path: $package_path, src_path: $src_path, toolchain: $toolchain, arch: $arch, custom_mirror: $custom_mirror"
if [[ "$package_path" == "" || ! -f "$package_path" ]]; then
  echo "File '$package_path' does not exist!"
  exit 1
fi

dockerfile_path_rwdi_extension=""
docker_arg_source_code=""
tag_extension=""
if [[ -n "$src_path" ]]; then
  cp -r "$src_path" "${working_dir}/src"
  dockerfile_path_rwdi_extension="_relwithdebinfo"
  docker_arg_source_code="--build-arg SOURCE_CODE=src"
  tag_extension="-relwithdebinfo"
fi

# Copy the .deb to working directory.
cp "$package_path" "${working_dir}/"
cd ${working_dir}

extension="${package_path##*.}"
dockerfile_path="${working_dir}/${toolchain_version}_${extension}${dockerfile_path_rwdi_extension}.dockerfile"
echo "Dockerfile: $dockerfile_path"
if [[ "$extension" == "deb" ]]; then
  # Extract version and offering from deb name.
  package_name=`echo $(basename "$package_path") | sed 's/.deb$//'`
  version=`echo ${package_name} | cut -d '_' -f 2 | rev | cut -d '-' -f 2- | rev | tr '+~' '__'`
elif [[ "$extension" == "rpm" ]]; then
  # Extract version and offering from rpm name.
  package_name=`echo $(basename "$package_path") | sed 's/.rpm$//'`
  version=`echo ${package_name} | cut -d '-' -f 2 | rev | cut -d '-' -f 2- | rev`
  version=${version%_1}
else
  echo "Invalid file sent as the package"
  print_help
  exit 1
fi

tag="${version}${tag_extension}"
image_name="memgraph/memgraph:${tag}"
image_package_name="memgraph-${tag}-docker.tar.gz"

# Build docker image.
# NOTE: --pull is here to always pull that latest base image because of security patches.
docker build --pull -t ${image_name} ${tag_latest} -f ${dockerfile_path} \
--build-arg BINARY_NAME=${package_name} \
--build-arg EXTENSION=${extension} ${docker_arg_source_code} \
--build-arg CUSTOM_MIRROR=$custom_mirror \
--build-arg TARGETARCH="" .
docker save ${image_name} ${latest_image} | gzip > ${image_package_name}
rm "${working_dir}/${package_name}.${extension}"
if [[ -n "$src_path" ]]; then
  rm -rf "${working_dir}/src"
fi

if [[ "$generate_sbom" == true ]]; then
  echo "Generating SBOM for Docker image"
  cd ../../
  ./tools/sbom/docker-sbom.sh "${tag}"
fi
docker image rm $image_name || true
echo "Built Docker image at '${working_dir}/${image_package_name}'"
